package cn.edu.xmu.oomall.customer.controller;

import cn.edu.xmu.oomall.core.model.VoObject;
import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.service.CustomerService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Jiazhe Yuan
 * @date 2021-12-14 12:13:04
 * @modifiedBy Jiazhe Yuan
 * @date 2021-12-14 13:24:51
 * @modifiedBy XJX
 * 2021-12-23 01:59:18
 * 修改getSimpleCustomerById为返回object
 */
@RestController
@RequestMapping(value = "", produces = "application/json;charset=UTF-8")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @Autowired
    private HttpServletResponse httpServletResponse;

    @GetMapping("/customers/states")
    public Object getAllCustomerStates() {
        ReturnObject<List> returnObject = customerService.getAllCustomerStates();
        return Common.decorateReturnObject(returnObject);
    }

    @Audit(departName = "departs")
    @GetMapping("/shops/{id}/customers")
    public Object getAllCustomers(@PathVariable Long id,
                                  @RequestParam(required = false) String userName,
                                  @RequestParam(required = false) String email,
                                  @RequestParam(required = false) String mobile,
                                  @RequestParam(required = false, defaultValue = "1") Integer page,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        if (!id.equals(0L)) {
            return Common.decorateReturnObject(new ReturnObject<>(ReturnNo.RESOURCE_ID_OUTSCOPE));
        }
        ReturnObject ret = null;
        ReturnObject<PageInfo<VoObject>> returnObject = customerService.getAllCustomers(userName, email, mobile, page, pageSize);
        ret = Common.getPageRetObject(returnObject);

        return Common.decorateReturnObject(ret);
    }

    @Audit(departName = "departs")
    @GetMapping("/shops/{shopId}/customers/{id}")
    public Object getCustomerByAdminWithId(@PathVariable Long shopId,
                                  @PathVariable Long id) {
        if (!shopId.equals(0L)) {
            return new ReturnObject<>(ReturnNo.RESOURCE_ID_OUTSCOPE);
        }

        ReturnObject ret = customerService.getCustomerByAdminWithId(id);
        return Common.decorateReturnObject(ret);
    }

    @GetMapping("/internal/customers/{id}")
    public Object getSimpleCustomerById(@PathVariable Long id) {
        ReturnObject ret = customerService.getCustomerInternal(id);
        return Common.decorateReturnObject(ret);
    }
}
